﻿Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports DotNetNuke.Services.Log.EventLog

Namespace LuceneSearch.Monitoring.Monitors
    Public Class IndexMergerMonitor
        Implements Interfaces.ILogInfoProvider

        Public Sub New(LPS As LucenePortalSettings)
            StorageTime = TimeSpan.FromSeconds(LPS.NbSecondsIndexing)
        End Sub

        Public Property ProcessedDocuments As Integer = 0

        Private Property StorageTime As TimeSpan
        Public Property AddedDocuments As Integer = 0
        Public Property UpdatedDocuments As Integer = 0
        Public Property AddWatch As New Stopwatch()
        Public ReadOnly Property StorageTimeExceeded As Boolean
            Get
                Return AddWatch.Elapsed > StorageTime
            End Get
        End Property

        Public Property DeleteWatch As New Stopwatch()
        Public Property DeletedForDuplication As Integer = 0
        Public Property DeletedForAge As Integer = 0
        Public Property DeletedForObsolescence As Integer = 0
        Public Property DeletedForDisappearing As Integer = 0
        Public ReadOnly Property DeletedDocuments() As Integer
            Get
                Return DeletedForDuplication + DeletedForAge + DeletedForObsolescence + DeletedForDisappearing
            End Get
        End Property

        Public Property IgnoredDocuments As Integer = 0
        Public Property SkippedDocuments As Integer = 0

        Public Function GetLogInfos() As System.Collections.Generic.IEnumerable(Of DotNetNuke.Services.Log.EventLog.LogInfo) Implements Interfaces.ILogInfoProvider.GetLogInfos
            Dim MergeLogInformations As New List(Of LogInfo)

            Dim MergeLogInfo As New LogInfo()
            MergeLogInfo.AddProperty("LuceneSearch storage", String.Format("Documents processed: {0}", ProcessedDocuments))
            MergeLogInfo.AddProperty("Documents added to index", AddedDocuments.ToString())
            MergeLogInfo.AddProperty("Documents updated in index", UpdatedDocuments.ToString())
            MergeLogInfo.AddProperty("Documents ignored (no changes)", IgnoredDocuments.ToString())
            MergeLogInfo.AddProperty("Documents skipped (not marked as processed)", SkippedDocuments.ToString())
            MergeLogInfo.AddProperty(String.Format("Time available for storage ({0}s) exceeded", StorageTime.TotalSeconds), StorageTimeExceeded.ToString())
            MergeLogInfo.AddProperty("Time elapsed during storage", String.Format("{0}s", AddWatch.Elapsed.TotalSeconds))
            MergeLogInfo.AddProperty("Documents deleted", DeletedDocuments.ToString())
            MergeLogInfo.AddProperty("- cause: duplication in the index", DeletedForDuplication.ToString())
            MergeLogInfo.AddProperty("- cause: old age in the index", DeletedForAge.ToString())
            MergeLogInfo.AddProperty("- cause: obsolete vs latest document version", DeletedForObsolescence.ToString())
            MergeLogInfo.AddProperty("- cause: is not present in the indexation results", DeletedForDisappearing.ToString())

            MergeLogInformations.Add(MergeLogInfo)
            Return MergeLogInformations.Skip(0) ' returns a true enumerable
        End Function
    End Class
End Namespace